package dp.gr03.payroll.generalTransactions;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;

import dp.gr03.payroll.Paycheck;
import dp.gr03.payroll.exception.PayRollException;
import dp.gr03.payroll.payrollApplication.Transaction;
import dp.gr03.payroll.payrollDatabase.PayrollDatabase;
import dp.gr03.payroll.payrollDomain.Employee;

public class PayDayTransaction implements Transaction
{
    private GregorianCalendar payDate;
    private HashMap<Integer, Paycheck> payChecks;

    public PayDayTransaction(GregorianCalendar payDate)
    {
        this.payDate = payDate;
        payChecks = new HashMap<Integer, Paycheck>();
    }

    @Override
    public void execute() throws PayRollException
    {
        ArrayList<Integer> empIds = new ArrayList<Integer>();
        PayrollDatabase database = PayrollDatabase.getInstance();
        database.getAllEmployeeIds(empIds);
        Employee e;
        for (Integer i : empIds)
        {
            if ((e = database.getEmployee(i)) != null)
            {
                if (e.isPayDay(payDate))
                {
                    Paycheck pc = new Paycheck(e.getPayPeriodStartDate(payDate), payDate);
                    payChecks.put(i, pc);
                    e.payDay(pc);
                }
            }
        }
    }

    public Paycheck getPackcheck(int empId)
    {
        return payChecks.get(empId);
    }
}
